package com.yc.jpyy.company.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.yc.jpyy.base.service.BaseService;
import com.yc.jpyy.base.util.PageBean;
import com.yc.jpyy.company.dao.CompanyDao;
import com.yc.jpyy.company.service.CompanyService;

@Service
public class CompanyServiceImpl extends BaseService implements CompanyService {
    
	@Autowired
	private CompanyDao companyDao;

	

	@Override
	public String addCompanyDao(Map<String, Object> companyInfo) throws Exception {
		//通过存储过程取编号,将值赋给当前新增记录
		String number = companyDao.getMaxNember(companyInfo);
		companyInfo.put("companyno", number);
		int ret = companyDao.addCompanyDao(companyInfo);
		String id = "";
		if(ret > 0 ){
			id = companyInfo.get("id").toString();
			/*//将ID的值赋给当前新增记录的companyNo
			int i = companyDao.UpdateCompanyNoById(companyInfo);
			if(i <= 0){
				throw new Exception();
			}*/
		}else{
			throw new Exception();
		}
		return id;
	}

	@Override
	public int updateCompanyDao(Map<String, Object> companyInfo) throws DataAccessException {
		//查询公司原来的名字
		String companyName = companyDao.selectCompanyName(companyInfo);
		//新公司的名字
		String newName= Optional.ofNullable(companyInfo.get("companyname")).orElse("").toString();
		int ret = companyDao.updateCompanyDao(companyInfo);
		if (ret > 0 && !companyName.equals(newName)){
			int i = companyDao.updateAllRelationName(companyInfo);
			return i;
		}else{
			return ret;
		}
	}

	@Override
	public int delCompanyServer(Map<String, Object> info) throws DataAccessException {
		int ret = companyDao.delCompanyDao(info);
		return ret;
	}
	
	
	@Override
	public int delforUpdateCompanyServer(Map<String, Object> info) throws DataAccessException {
		int ret = companyDao.delforupdateCompanyDao(info);
		return ret;
	}
	//测试事务处
	//@Override
	//public int addAndDel(Map<String, Object> companyInfo) throws DataAccessException {
		//int aa = companyDao.addCompanyDao(companyInfo);
		//System.out.println(4/0);
		//int ret = companyDao.delCompanyDao(36);
		//return ret;
	//}

	@Override
	public Map<String, Object> findCompanyByID(Map<String, Object> info) throws DataAccessException {
	     return companyDao.findCompanyByID(info);
	}

	

	@Override
	public PageBean findCompanyDao(Map<String, Object> paramsMap) throws DataAccessException {
		int pagenum = Integer.parseInt(paramsMap.get("pagenum").toString());
		int pagesize = Integer.parseInt(paramsMap.get("pagesize").toString());
		paramsMap.put("pagestart", (pagenum-1) * pagesize);
		String companyid = Optional.ofNullable(paramsMap.get("companyid")).orElse("").toString();
		paramsMap.put("companyid", companyid.equals("")?"":companyid.split(","));
		List<Map<String, Object>> list = companyDao.findCompanyDao(paramsMap);
		PageBean pageBean = new PageBean();
		pageBean.setList(list);
		// 每页几条数据，默认20条
		pageBean.setPageSize(pagesize);
		// 得到数据库中的条数
		pageBean.setAllRow(this.findCompanyDaoCount(paramsMap));
		// 当前第几页，默认第一页
		pageBean.setCurrentPage(pagenum);
		// 得到一共几页
		pageBean.setTotalPage(pageBean.getTotalPage());
		// 模仿百度分页，只显示10条数据
		pageBean.paginationTool(pagenum, pageBean.getTotalPage());
		return pageBean;
	}

	@Override
	public int findCompanyDaoCount(Map<String, Object> paramsMap) throws DataAccessException {
		return companyDao.findCompanyDaoCount(paramsMap);
	}

	@Override
	public int findExistsByNameServer(Map<String, Object> paramsMap) throws DataAccessException {
		return companyDao.findExistsByNameDao(paramsMap);
	}

	
	
	
	
	@Override
	public List findCompanyNameServer(Map<String, Object> paramsMap) throws DataAccessException {
		String companyid = Optional.ofNullable(paramsMap.get("companyid")).orElse("").toString();
		paramsMap.put("companyid", companyid.equals("")?"":companyid.split(","));
		List<Map<String, Object>> list =  companyDao.findCompanyNameDao(paramsMap);
		return list;
	}

	@Override
	public List<Map<String, Object>> findSumDriverCountAndSumCarCountByID(Map<String, Object> paramsMap)
			throws DataAccessException {
		// TODO Auto-generated method stub
		return companyDao.findSumDriverCountAndSumCarCountByID(paramsMap);
	}

	@Override
	public Map<String, Object> getArchivesNo(Map<String, Object> paramsMap)
			throws DataAccessException {
		Map<String,Object> resultMap = new HashMap<>();
		String str = companyDao.getArchivesNo(paramsMap);
		if(!str.isEmpty() && !str.equals("")){
			resultMap.put("returnCode", 1);
			resultMap.put("returnMsg", "查询成功");
			resultMap.put("ListData",str);
		}else{
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "查询失败");
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> findCompanySelect(Map<String, Object> paramsMap)
			throws DataAccessException {
		Map<String,Object> resultMap = new HashMap<>();
		int pagenum = Integer.parseInt(paramsMap.get("pagenum").toString());
		int pagesize = Integer.parseInt(paramsMap.get("pagesize").toString());
		paramsMap.put("pagestart", (pagenum-1) * pagesize);
		String companytype = Optional.ofNullable(paramsMap.get("companytype")).orElse("").toString();
		paramsMap.put("companytypeArray", companytype.equals("")?companytype:companytype.split(","));
		List<Map<String,Object>> list = companyDao.findCompanySelect(paramsMap);
		int counts = companyDao.findCompanySelectCount(paramsMap);
		if(list != null && list.size() > 0){
			resultMap.put("returnCode", 1);
			resultMap.put("ListData",list);
			resultMap.put("TotalCount",counts);
			resultMap.put("TotalPage",counts % pagesize >0 ? counts/pagesize + 1 : counts/pagesize);
			resultMap.put("returnMsg", "操作成功");
		}else{
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "系统无数据");
		}
		return resultMap;
	}
	
	@Override
	public Map<String, Object> updateCompanyLicenseDate(Map<String, Object> paramsMap)
			throws DataAccessException {
		Map<String,Object> resultMap = new HashMap<>();
		int i = companyDao.updateCompanyLicenseDate(paramsMap);
		if(i > 0){
			resultMap.put("returnCode", 1);
			resultMap.put("returnMsg", "修改成功");
		}else{
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "修改失败");
		}
		return resultMap;
	}

		
}
